//
//  ZZCameraSelectVC.m
//  ZZBlue
//
//  Created by stonesMonkey on 2017/9/19.
//  Copyright © 2017年 stonesMonkey. All rights reserved.
//

#import "ZZCameraSelectVC.h"
#import "ZZCameraTool.h"
#import "ZZAORBView.h"
#import "GetColorViewController.h"
@interface ZZCameraSelectVC ()

@property (nonatomic, strong)UIButton *titleBtn;

@property (weak, nonatomic) IBOutlet UIImageView *bgImage;

@property (weak, nonatomic) IBOutlet UIImageView *signImage;


@property (weak, nonatomic) IBOutlet UIView *testViw;

@property (nonatomic, strong)ZZAORBView *AorBView;

@property (weak, nonatomic) IBOutlet UIImageView *leftbottomImage;
@property (weak, nonatomic) IBOutlet UIImageView *rightbottomImage;

@property (nonatomic, strong)UIImage *bgImagePhoto;
@property (nonatomic, strong)UIImage *bgImageCamera;


@property (weak, nonatomic) IBOutlet UIView *colorLineView;


@end

@implementation ZZCameraSelectVC

//-(void)viewDidLayoutSubviews {
//    [super viewDidLayoutSubviews];
//
//    self.AorBView.frame = CGRectMake(0, 0, 189, 33);
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    ZZAORBView *AorBView = [[ZZAORBView alloc] initWithFrame:CGRectMake(0, 0, 189, 33)];
    AorBView.backgroundColor = [UIColor clearColor];
    //    UIView *view = [UIView new];
    //    view.backgroundColor = [UIColor redColor];
    //    view.frame = CGRectMake(0, 0, 189, 33);
    
    AorBView.AorBClick = ^(NSInteger tag){
        
        if (tag == 1000) {
            
            self.leftbottomImage.image = [UIImage imageNamed:@"img_tpqs_s"];
            self.rightbottomImage.image = [UIImage imageNamed:@"img_ssqs_s"];
            //            self.bgImage.image = self.bgImagePhoto;
        } else {
            
            self.leftbottomImage.image = [UIImage imageNamed:@"img_tpqs_s"];
            self.rightbottomImage.image = [UIImage imageNamed:@"img_ssqs_s"];
            //            self.bgImage.image = self.bgImageCamera;
            
        }
    };
    //    [AorBView layoutIfNeeded];
    //    [view addSubview:AorBView];
    self.AorBView =  AorBView;
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"icon_fanh"] forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 0, 32, 32);
    [btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchDown];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setImage:[UIImage imageNamed:@"icon_fanh"] forState:UIControlStateNormal];
    rightBtn.frame = CGRectMake(0, 0, 32, 32);
    [rightBtn addTarget:self action:@selector(topRightClick) forControlEvents:UIControlEventTouchDown];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    self.bgImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    self.bgImage.layer.masksToBounds = YES;
    //    self.bgImage.contentMode = UIViewContentModeScaleAspectFit;
    [self.bgImage addGestureRecognizer:tap];
    
    UIPanGestureRecognizer *pan2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(btn2Move:)];
    self.bgImage.userInteractionEnabled = YES;
    [self.bgImage addGestureRecognizer:pan2];
}

- (void)btn2Move:(UIPanGestureRecognizer *)pan {
    
    CGPoint  point = [pan translationInView:self.bgImage];
    CGFloat width = self.bgImage.width - self.signImage.width / 2;
    
    CGRect originalFrame = self.signImage.frame;
    
    originalFrame.origin.x += point.x;
    originalFrame.origin.y += point.y;
    
    // 圆心
    CGPoint center = CGPointMake(CGRectGetMidX(originalFrame) - width / 2,CGRectGetMidY(originalFrame) - width /2);
    
    NSLog(@"%lf",originalFrame.origin.y);
    if (originalFrame.origin.x < -self.signImage.width / 2) {
        //
        originalFrame.origin.x = -self.signImage.width / 2;
    } else if (originalFrame.origin.x > Z_SCREEN_WIDTH -self.signImage.width / 2) {
        
        originalFrame.origin.x = Z_SCREEN_WIDTH -self.signImage.width / 2;
    }
    
    if (originalFrame.origin.y < self.bgImage.y -self.signImage.height / 2) {
        //
        originalFrame.origin.y = self.bgImage.y -self.signImage.height / 2;
    } else if (originalFrame.origin.y > CGRectGetMaxY(self.bgImage.frame) - self.signImage.height / 2) {
        
        originalFrame.origin.y = CGRectGetMaxY(self.bgImage.frame) - self.signImage.height / 2;
    }
    //
    
    self.signImage.frame = originalFrame;
    [pan setTranslation:CGPointZero inView:self.bgImage];
    
    switch (pan.state) {
            
        case UIGestureRecognizerStateChanged:
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateFailed:
        case UIGestureRecognizerStateCancelled:
        {
            // 输出当前ImageView上的颜色值
            UIColor*color = [self colorOfPoint:self.signImage.center];
            self.colorLineView.backgroundColor = color;
            if (self.changeColor ) {
                
                
                self.changeColor(color, self.AorBView.selectTag == 1000?NO:YES);
            }
            //            NSLog(@"%@",color);
            
            
        }
            break;
            
        default:
            break;
    }
}


- (void)tapGesture:(UITapGestureRecognizer *)tap{
    
    CGPoint point = [tap locationInView:self.bgImage];
    
    CGPoint point2 = [tap locationInView:self.view];
    UIColor *color = [self colorOfPoint:point];
    self.signImage.center = point2;
    self.testViw.backgroundColor = color;
    self.colorLineView.backgroundColor = color;
    if (self.changeColor) {
        
        self.changeColor(color,self.AorBView.selectTag == 1000? NO:YES);
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if (self.navigationItem.titleView == nil) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            UIView *view = [[UIView alloc] init];
            view.backgroundColor = [UIColor redColor];
            view.frame = CGRectMake(0, 0, 190, 33);
            self.navigationItem.titleView = self.AorBView;
            [view addSubview:self.AorBView];
            self.navigationItem.titleView.frame = CGRectMake(0, 0, 190, 33);
        });
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - action
- (IBAction)leftBtnClick:(id)sender {
    
    [[ZZCameraTool new]userPhotoLibrary:self result:^(UIImage * _Nullable image, NSError * _Nullable error) {
        
        if (error == nil) {
            
            self.titleBtn.selected = NO;
            
            self.bgImage.image = image;
            [self.AorBView selelctIndex:1000];
        } else {
            
            [MBProgressHUD zAlertError:error.userInfo[@"message"]];
        }
    }];
}

- (IBAction)rightBtnClick:(UIButton *)sender {
    
    Z_WEAK_SELF;
    GetColorViewController *vc = [[GetColorViewController alloc] init];
    
    
    
    vc.outputColor = ^(UIColor *color,UIImage *image) {
        
        self.signImage.center = self.bgImage.center;
        if (weakSelf.changeColor) {
            
            
            weakSelf.changeColor(color, self.AorBView.selectTag == 1000?NO:YES);
            
            if (self.AorBView.selectTag == 1000) {
                
                self.bgImage.image = image;
            } else {
                
                self.bgImage.image = image;
            }
        }
    };
    
    [self presentViewController:vc animated:YES completion:^{
        
    }];
    
}

- (void)topRightClick {
    
    
}

- (void)titleBtnDidClick {
    
    self.titleBtn.selected  = !self.titleBtn.selected;
    
    if(self.titleBtn.selected == YES) { // 相册
        
        self.bgImage.image = [UIImage imageNamed:@"img02"];
    } else {
        
        self.bgImage.image = [UIImage imageNamed:@"img01"];
    }
    
}

- (void)back {
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Func
- (UIColor *)colorOfPoint:(CGPoint)point {
    
    unsigned char pixel[4] = {0};
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
    
    CGContextTranslateCTM(context, -point.x, -point.y);
    
    [self.view.layer renderInContext:context];
    
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    
    UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0];
    
    return color;
}

- (UIImage *)bgImagePhoto {
    
    if (!_bgImagePhoto) {
        
        _bgImagePhoto = [UIImage imageNamed:@"img01"];
    }
    return _bgImagePhoto;
}

- (UIImage *)bgImageCamera {
    
    if (!_bgImageCamera) {
        
        _bgImageCamera = [UIImage imageNamed:@"img02"];
    }
    return _bgImageCamera;
}


@end

